package com.puzzlebazar.client.util;

import com.google.gwt.user.client.ui.Hyperlink;
import com.puzzlebazar.client.main.view.UpdateTokenCallback;

public class AutoHyperlink extends Hyperlink implements TokenFactory, UpdateTokenCallback {

	private final TokenFactory tokenFactory;
	

	/**
	 * Creates a hyperlink with its text and a factory to automatically
	 * update its history token.
	 * 
	 * @param text the hyperlink's text
	 * @param asHTML <code>true</code> to treat the specified text as html
	 * @param tokenFactory the factory that can create the token
	 */
	public AutoHyperlink(String text, boolean asHTML, TokenFactory tokenFactory) {
		this.tokenFactory = tokenFactory;
	    if (asHTML) {
			setHTML(text);
		} else {
			setText(text);
		}
		updateTokens();
	}

	/**
	 * Creates a hyperlink with its text and a factory to automatically
	 * update its history token.
	 * 
	 * @param text the hyperlink's text
	 * @param tokenFactory the factory that can create the token
	 */
	public AutoHyperlink(String text, TokenFactory tokenFactory) {
		this.tokenFactory = tokenFactory;
		setText( text );
		updateTokens();
	}
	
	@Override
	public String createToken() {
		return tokenFactory.createToken();
	}

	@Override
	public void updateTokens() {
		String historyToken = createToken();
		if( historyToken == null )
			setVisible( false );
		else
			setTargetHistoryToken( historyToken );		
	}

}
